package com.mulanweb.utils;

import com.jfinal.kit.StrKit;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by mulan on 2016/10/11.
 */
public class DateUtil
{
    /**
     *将Date格式化 为 yyyy-MM-dd HH:mm:ss
     * @param currentTime 待转换的Date
     * @return yyyy-MM-dd HH:mm:ss String
     */
    public static String dateToStr(Date currentTime){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 将Date格式化 为 yyyy-MM-dd
     * @param currentTime 待转化的Date
     * @return yyyy-MM-dd  String
     */
    public static String dataToYyyyMmDd(Date currentTime){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 将Date格式化 为 HH:mm:ss
     * @param currentTime 待转化的Date
     * @return yyyy-MM-dd  String
     */
    public static String dataToHhMmSs(Date currentTime){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 根据Date获取星期几
     * @param currentTime 待转化的Date
     * @return yyyy-MM-dd  String
     */
    public static String getWeek(Date currentTime){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        String tt = dateFm.format(currentTime) ;
       return tt;
    }

    /**
     * 根据Date获取星期几 并转换成int类型
     * @param currentTime 待转化的Date
     * @return yyyy-MM-dd  String
     */
    public static Integer getWeekToInt(Date currentTime){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        Integer test = null ;
        String week = getWeek(currentTime);
        if("星期一".equals(week)){
            test=1;
        }else if("星期二".equals(week)){
            test=2;
        }else if("星期三".equals(week)){
            test=3;
        }else if("星期四".equals(week)){
            test=4;
        }else if("星期五".equals(week)){
            test=5;
        }else if("星期六".equals(week)){
            test=6;
        }else if("星期日".equals(week)){
            test=7;
        }
        return test ;
    }

    public static Integer getWeekToInt(String currentTime){
        return getWeekToInt(strToDate(currentTime));
    }

    /**
     * 将当前日期加一天
     * @param currentTime
     * @return 当前日期的后一天
     */
    public static Date addDate(Date currentTime,Integer day){
        if(!StrKit.notNull(currentTime)){
            return null;
        }
        Calendar calendar   =   new GregorianCalendar();
        calendar.setTime(currentTime);
        calendar.add(calendar.DATE,day == null?1:day);//把日期往后增加一天.整数往后推,负数往前移动
        currentTime=calendar.getTime();
        return currentTime;
    }

    /**
     * 将当前日期加一天
     * @param currentTime
     * @return 当前日期的后一天
     */
    public static Date addDate(Date currentTime){
        return addDate(currentTime,1);
    }


    /**
     * String 类型  转换成 Date 类型 (yyyy-MM-dd HH:mm)
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate){
        return strToDate(strDate,"yyyy-MM-dd HH:mm");
    }

    public static Date strToDate(String strDate, String formatter){
        if(StrKit.isBlank(strDate)){
            return null;
        }
        try{
            SimpleDateFormat sdf = new SimpleDateFormat(formatter);
            Date date = sdf.parse(strDate);
            return date;
        }catch(ParseException e){
            e.printStackTrace();
        }
        return null;
    }

    public static Boolean compareTime(String date1, String date2) {
        DateFormat df = new SimpleDateFormat("hh:mm");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() >= dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return true;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return false;
            } else {
                return null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws ParseException{
        int i = 10 ;
        int y =0;

        while(y<i){
            System.out.println("---:"+y);
            for(int t =0;t<i;t++){
                System.out.println("------"+t);
                if(t==4){
                    break;
                }
            }
            y++;
        }

        //compareTime("15:21", "09:59");//获取两个日期间的差 */

        /*String s = "2016-10-01";
        String e= "2016-10-18";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(s);

        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        Date endTime = sdf.parse(e);

        String startStr = DateUtil.dataToYyyyMmDd(startTime);
        String endStr = DateUtil.dataToYyyyMmDd(endTime);

        List<Date> dateList = new ArrayList<>();

        while(!(endStr.equals(startStr))){

            dateList.add(startTime);
            System.out.println(dataToYyyyMmDd(startTime));
            startTime =DateUtil.addDate(startTime);
            startStr=DateUtil.dataToYyyyMmDd(startTime);
        }*/
    }

}
